#include "core.cpp"

int findRangeMinima(CvHistogram *hist, int ind1, int ind2)
{
  return (ind1+ind2)/2; // TODO: Implement this correctly
}

int getValleyColor(CvHistogram *hist)
{
  int highInd=0,secondHighInd=0;
  float highVal=0.0f,secondHighVal=0.0f;
  for(int i=0;i<255;i++)
  {
    float histValue = cvQueryHistValue_1D(hist, i);
    if(histValue>=highVal)
    {
      secondHighVal = highVal;
      highVal = histValue;
      secondHighInd = highInd;
      highInd = i;
    }
    else if(histValue>secondHighVal)
    {
      secondHighVal = histValue;
      secondHighInd = i;
    }
  }
  return findRangeMinima(hist,highInd,secondHighInd);
}

int main(int argc, char *argv[])
{
  IplImage *img=NULL;
  if(argc<2)
  {
    printf("Invalid argc\n");
    exit(-1);
  }
  int i=atoi(argv[1]);
  int n = atoi(argv[2]);
  char file[20];
  sprintf(file,"images/img%d.jpg",i);
  img=cvLoadImage(file);
  if(!img){
    printf("Could not load image file\n");
    exit(0);
  }


  IplImage* newimg;
  newimg=cvCreateImage(cvSize(img->width,img->height),img->depth,img->nChannels);
  cvCopy(img,newimg,NULL);
  subtractBackground(newimg);
  thinChars(newimg,n);

   // RGB to Grayscale to Binary
  IplImage* grayImg = cvCreateImage( cvSize(newimg->width, newimg->height), IPL_DEPTH_8U, 1 );
  IplImage* final1 = cvCreateImage( cvSize(newimg->width, newimg->height), IPL_DEPTH_8U, 1 );
  IplImage* final2 = cvCreateImage( cvSize(newimg->width, newimg->height), IPL_DEPTH_8U, 1 );
  IplImage* final3 = cvCreateImage( cvSize(newimg->width, newimg->height), IPL_DEPTH_8U, 1 );
  cvCvtColor(newimg, grayImg, CV_BGR2GRAY );
  cvThreshold(grayImg, grayImg, 1, 255, CV_THRESH_BINARY );
//   getSkeletons(newimg,final,n);
    
  IplImage* hsvimg=cvCreateImage(cvGetSize(img), 8, 3);
  cvCvtColor(newimg,hsvimg,CV_BGR2HSV);
  IplImage* img1 = cvCreateImage(cvGetSize(img), 8, 1);
  IplImage* img2 = cvCreateImage(cvGetSize(img), 8, 1);
  IplImage* img3 = cvCreateImage(cvGetSize(img), 8, 1);
  cvSplit(newimg, img1, img2, img3, NULL);
  
  getSkeletons1Channel(img1,final1,1);
  getSkeletons1Channel(img2,final2,1);
  getSkeletons1Channel(img3,final3,1);
  
  // GetHistograms
//   int numBins = 256;
//   float range[] = {0, 255};
//   float *ranges[] = { range };

//   CvHistogram *hist1 = cvCreateHist(1, &numBins, CV_HIST_ARRAY, ranges, 1);
//   CvHistogram *hist2 = cvCreateHist(1, &numBins, CV_HIST_ARRAY, ranges, 1);
//   CvHistogram *hist3 = cvCreateHist(1, &numBins, CV_HIST_ARRAY, ranges, 1);
//   cvClearHist(hist1);
//   cvClearHist(hist2);
//   cvClearHist(hist3);
//   cvCalcHist(&img1, hist1, 0, 0);
//   cvCalcHist(&img2, hist2, 0, 0);
//   cvCalcHist(&img3, hist3, 0, 0);
//   int maxValue = 100;
//   IplImage *histImg1 = cvCreateImage(cvSize(256, maxValue), 8 ,1);
//   IplImage *histImg2 = cvCreateImage(cvSize(256, maxValue), 8 ,1);
//   IplImage *histImg3 = cvCreateImage(cvSize(256, maxValue), 8 ,1);
//   plotHistogram1D(hist1,histImg1,maxValue);
//   plotHistogram1D(hist2,histImg2,maxValue);
//   plotHistogram1D(hist3,histImg3,maxValue);
//   
  // create a window
  cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE); 
  cvMoveWindow("mainWin", 100, 100);
  cvNamedWindow("mainWin1", CV_WINDOW_AUTOSIZE); 
  cvMoveWindow("mainWin1", 400, 100);
  cvNamedWindow("mainWin2", CV_WINDOW_AUTOSIZE); 
  cvMoveWindow("mainWin2", 700, 100);
  
  cvNamedWindow("img1", CV_WINDOW_AUTOSIZE); 
  cvMoveWindow("img1", 100, 400);
  cvNamedWindow("img2", CV_WINDOW_AUTOSIZE); 
  cvMoveWindow("img2", 400, 400);
  cvNamedWindow("img3", CV_WINDOW_AUTOSIZE); 
  cvMoveWindow("img3", 700, 400);
  cvShowImage("img1", img1);
  cvShowImage("img2", img2);
  cvShowImage("img3", img3);
  
  // show the image
  cvShowImage("mainWin", img );
  cvShowImage("mainWin1", grayImg );
  cvShowImage("mainWin2", newimg );
  
  cvNamedWindow("final1", CV_WINDOW_AUTOSIZE); 
  cvMoveWindow("final1", 100, 600);
  cvNamedWindow("final2", CV_WINDOW_AUTOSIZE); 
  cvMoveWindow("final2", 400, 600);
  cvNamedWindow("final3", CV_WINDOW_AUTOSIZE); 
  cvMoveWindow("final3", 700, 600);
  cvShowImage("final1", final1 );
  cvShowImage("final2", final2 );
  cvShowImage("final3", final3 );

  // wait for a key
  cvWaitKey(0);

  // release the image
  cvReleaseImage(&img );
  return 0;
}


